`pbjson-build` consumes the descriptor output of [`prost-build`][1] and generates
[`serde::Serialize`][2] and [`serde::Deserialize`][3] implementations
that are compliant with the [protobuf JSON mapping][4]
# Usage
_It is recommended you first follow the example in [prost-build][1] to familiarise
yourself with `prost`_
Add `prost-build`, `prost`, `pbjson`, `pbjson-build` and `pbjson-types` to
your `Cargo.toml`
```toml
[dependencies]
prost =
pbjson =
pbjson-types =
[build-dependencies]
prost-build =
pbjson-build =
```
Next create a `build.rs` containing the following
```ignore
// This assumes protobuf files are under a directory called `protos`
// and in a protobuf package `mypackage`
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let proto_files = vec![root.join("myproto.proto")];
// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files {
println!("cargo:rerun-if-changed={}", proto_file.display());
}
let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap())
.join("proto_descriptor.bin");
prost_build::Config::new()
// Save descriptors to file
.file_descriptor_set_path(&descriptor_path)
// Override prost-types with pbjson-types
.compile_well_known_types()
.extern_path(".google.protobuf", "::pbjson_types")
// Generate prost structs
.compile_protos(&proto_files, &[root])?;
let descriptor_set = std::fs::read(descriptor_path)?;
pbjson_build::Builder::new()
.register_descriptors(&descriptor_set)?
.build(&[".mypackage"])?;
```
Finally within `lib.rs`
```ignore
/// Generated by [`prost-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.rs"));
/// Generated by [`pbjson-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.serde.rs"));
```
The module will now contain the generated prost structs for your protobuf definition
along with compliant implementations of [serde::Serialize][2] and [serde::Deserialize][3]
[1]: https://docs.rs/prost-build
[2]: https://docs.rs/serde/1.0.130/serde/trait.Serialize.html
[3]: https://docs.rs/serde/1.0.130/serde/trait.Deserialize.html
[4]: https://developers.google.com/protocol-buffers/docs/proto3#json